package com.wanmait.baohan.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/doooooo")
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws  IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        Object user = httpServletRequest.getSession().getAttribute("user");

        if (user == null ) {
            httpServletRequest.getRequestDispatcher("/user/login").forward(servletRequest, servletResponse);
            return;
            //httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"login");
            //httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/login.jsp");
        }else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
}
